package spring.chatroom.java_chatroom.common.utils;

import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;


/**
 * 将webSession和userId映射起来 不能使用sessionId 因为分辨不出是谁的webSession
 * 改为redis
 */
public class WebSocketSessionManagerUtil {

    static ConcurrentHashMap<Integer, WebSocketSession> sessionMap = new ConcurrentHashMap<>();

    public static void setAttribute(Integer userId, WebSocketSession session) {
        //todo 同一个id多个Session的处理
        sessionMap.put(userId, session);
    }


    public static ConcurrentHashMap<Integer, WebSocketSession> getMap() {
        return sessionMap;
    }

    public static WebSocketSession getAttribute(Integer UserId) {
        return sessionMap.get(UserId);
    }


    public static void removeAttribute(Integer userId) {
        if (userId == 0) {
            return;
        }
        sessionMap.remove(userId);
    }

}
